package com.kunlun.basedata.controller;

import com.kunlun.basedata.model.vo.SystemFileVo;
import com.kunlun.basedata.service.ISystemFileService;
import com.kunlun.common.model.Page;
import com.kunlun.common.utils.ResponseUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(value = "/systemFile")
public class SystemFileController {

    private Logger log = LogManager.getLogger();

    @Autowired
    private ISystemFileService systemFileService;

    @RequestMapping(value = "/getAllFiles", method = RequestMethod.GET)
    public Object getAllFiles(SystemFileVo systemFileVo, int currentPage, int pageSize) {
        try {
            Page roles = systemFileService.getAllFiles(systemFileVo, currentPage, pageSize);
            return ResponseUtil.successResponse(roles);
        } catch (Exception e) {
            log.error("RoleController getAllRole Error: ", e);
            return ResponseUtil.failedResponse("查询所有角色失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/onUpload", method = RequestMethod.POST)
    public Object onUpload(@RequestParam("file") MultipartFile file) {
        try {
            systemFileService.onUpload(file);
            return ResponseUtil.successResponse("上传成功");
        } catch (Exception e) {
            log.error("SystemFileController onUpload Error: ", e);
            return ResponseUtil.failedResponse("上传失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/onDelete", method = RequestMethod.POST)
    public Object onDelete(String ids) {
        try {
            systemFileService.onDelete(ids);
            return ResponseUtil.successResponse("删除成功");
        } catch (Exception e) {
            log.error("SystemFileController onDelete Error: ", e);
            return ResponseUtil.failedResponse("删除失败！", e.getMessage());
        }
    }
}
